package com.gennlife.framework.task;

import com.gennlife.common.utils.PropertiesUtil;
import com.gennlife.dataformat.controller.OperationController;
import com.gennlife.dataformat.mapper.OperationMapper;
import com.gennlife.dataformat.service.ICUService;
import com.gennlife.dataformat.service.OperationService;
import com.gennlife.framework.domain.TaskInfo;
import com.smqj.icu.File2Gp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.script.ScriptException;
import java.io.File;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.ResourceBundle;

@Service
@PropertySource(value = "classpath:application.yml")
public class AutoUpdateDateSchedule {

    private static final Logger logger = LoggerFactory.getLogger(AutoUpdateDateSchedule.class);

    @Autowired
    private OperationService operationService;

    @Scheduled(cron = "${cron.importIcu}")
    public void importIcuTask() throws SQLException, ScriptException, NoSuchMethodException {
        if (ResourceBundle.getBundle("icu").getString("auto.import").equals("yes")) {
            Long time = System.currentTimeMillis();
            File2Gp gp = new File2Gp();
            gp.dealFiles2Gp("1,2,3", "/bak/MAIN", 1);
            logger.info("定时导入ICU数据成功，耗时：" + (System.currentTimeMillis() - time));
        }
//        TaskInfo info = new TaskInfo();
//        info.setTaskResolveCode("");
//        info.setTaskFileDir("C:\\Users\\daotian\\Desktop\\resolve");
//        info.setTaskSaveType("csv");
//        operationService.resolveDataAll(info);
    }

    @Scheduled(cron = "${cron.importPage}")
    public void importPageTask() {
        if (ResourceBundle.getBundle("page").getString("auto.import").equals("yes")) {
            String sourceFolder = PropertiesUtil.getValue("page.sourceFolder");
            File file = new File(sourceFolder);
            String[] fs = file.list();    //遍历path下的文件和目录，放在File数组中
            logger.info("定时导入病案首页数据，一共需要处理" + (fs.length - 2) + "个文件。");
            long time1 = System.currentTimeMillis();
            HashSet<String> importFileNames = new HashSet<>(Arrays.asList(fs));
            for (String fileName : fs) {
                operationService.resolveData(new File(sourceFolder + "/" + fileName));
            }
            logger.info("定时导入病案首页数据，本次导入一共耗时：" + ((System.currentTimeMillis() - time1) / 1000) + "秒");
        }
    }

    @Scheduled(cron = "${cron.importIcuExcel}")
    public void importIcuExcelTask() throws SQLException {
        if (ResourceBundle.getBundle("icu").getString("auto.import").equals("yes")) {
            Long time = System.currentTimeMillis();
            ICUService gp = new ICUService();
            gp.dealFiles2Gp("1,2,3", "D:/bak/MAIN", 1);
            logger.info("定时导入ICU数据成功，耗时：" + (System.currentTimeMillis() - time));
        }
//        TaskInfo info = new TaskInfo();
//        info.setTaskResolveCode("");
//        info.setTaskFileDir("C:\\Users\\daotian\\Desktop\\resolve");
//        info.setTaskSaveType("csv");
//        operationService.resolveDataAll(info);
    }
}
